﻿#region License


// Virtual Essentials - http://www.Virtual-Essentials.com
// Copyright (c) 2005-2013
// 
//  This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

using DotNetNuke;
using DotNetNuke.Entities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.UI.UserControls;

namespace VirtualEssentials.DNN.Modules.Testimonials
{
    public partial class Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase
    {

 
        protected void CreateControls()
        {
            try
            {
                HtmlGenericControl veDnnForm = new HtmlGenericControl();
                veDnnForm.Attributes.Add("class", "dnnForm dnnClear");
                Page.Controls.Add(veDnnForm);

                HtmlGenericControl dnnFormItem1 = new HtmlGenericControl();
                dnnFormItem1.Attributes.Add("class", "dnnFormItem");
                veDnnForm.Controls.Add(dnnFormItem1);

                System.Web.UI.Control oControl = new System.Web.UI.Control();
                oControl = (DotNetNuke.UI.UserControls.LabelControl)LoadControl("~/controls/LabelControl.ascx");
                oControl.ID = "lblDisplayTemplate";
                dnnFormItem1.Controls.Add(oControl);

                DotNetNuke.UI.UserControls.LabelControl lblDisplayTemplate = (LabelControl)dnnFormItem1.FindControl("lblDisplayTemplate");
                if ((lblDisplayTemplate != null))
                {
                    lblDisplayTemplate.ResourceKey = "lblDisplayTemplate";
                    lblDisplayTemplate.Suffix = ":";
                    lblDisplayTemplate.ControlName = "txtDisplayTemplate";
                }

                TextBox txtDisplayName = new TextBox();
                txtDisplayName.ID = "txtDisplayName";
                txtDisplayName.CssClass = "dnnFormRequired";
                txtDisplayName.TextMode = TextBoxMode.MultiLine;
                dnnFormItem1.Controls.Add(txtDisplayName);

                //System.Web.UI.Control oOrderByControl = new System.Web.UI.Control();
                //oOrderByControl = (DotNetNuke.UI.UserControls.LabelControl)LoadControl("~/controls/LabelControl.ascx");
                //oOrderByControl.ID = "lblOrderBy";
                //dnnFormItem1.Controls.Add(oOrderByControl);

                //DotNetNuke.UI.UserControls.LabelControl lblOrderBy = (LabelControl)dnnFormItem1.FindControl("lblOrderBy");
                //if ((lblDisplayTemplate != null))
                //{
                //    lblDisplayTemplate.ResourceKey = "lblOrderBy";
                //    lblDisplayTemplate.Suffix = ":";
                //    lblDisplayTemplate.ControlName = "txtOrderBy";
                //}




            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Load the settings for the module
        /// </summary>
        public override void LoadSettings()
        {
            try
            {
               // CreateControls();

                if (!IsPostBack)
                {

                    //sets the display template for each item
                    if (!String.IsNullOrEmpty((string)ModuleSettings["DisplayTemplate"]))
                    {
                        txtDisplayTemplate.Text = (string)ModuleSettings["DisplayTemplate"];
                    }else{
                        //Build out a default value
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<div class=\"testimonialContainer\">");
                        sb.AppendLine("<div class=\"testimonial\">");
                        sb.AppendLine("[Testimonial] <br />");
                        sb.AppendLine("[FirstName][LastNameInitial] <br />");
                        sb.AppendLine("[Location]");
                        sb.AppendLine("</div>");
                        sb.AppendLine("</div>");
                        txtDisplayTemplate.Text = sb.ToString();
                    }


                    if (!String.IsNullOrEmpty((string)ModuleSettings["ContainerClass"]))
                    {
                        txtContainerClass.Text = (string)ModuleSettings["ContainerClass"];
                    }
                    else
                    {
                        txtContainerClass.Text = "veTestimonialRotator"; 
                    }

                    if (!String.IsNullOrEmpty((string)ModuleSettings["AddNewLinkClass"]))
                    {
                        txtAddNewLinkClass.Text = (string)ModuleSettings["AddNewLinkClass"];
                    }
                    else
                    {
                        txtAddNewLinkClass.Text = "veTestimonialAddNewLink"; 
                    }

                    if (!String.IsNullOrEmpty((string)ModuleSettings["AddNewLinkText"]))
                    {
                        txtAddNewLinkText.Text = (string)ModuleSettings["AddNewLinkText"];
                    }
                    else
                    {
                        txtAddNewLinkText.Text = "Add a Testimonial"; 
                    }

                    if (!String.IsNullOrEmpty((string)ModuleSettings["NumberToDisplay"]))
                    {
                        txtNumberToDisplay.Text = (string)ModuleSettings["NumberToDisplay"];
                    }
                    else
                    {
                        txtNumberToDisplay.Text = "10"; //default 
                    }

                    if (!String.IsNullOrEmpty((string)ModuleSettings["AnimationOption"]))
                    {
                        ddlAnimationOption.SelectedValue = (string)ModuleSettings["AnimationOption"];
                    }
                    else
                    {
                        ddlAnimationOption.SelectedValue = "vetestimonials.js"; 
                    }


                    if (!String.IsNullOrEmpty((string)ModuleSettings["AddNewPlacement"]))
                    {
                        ddlAddNewPlacement.SelectedValue = (string)ModuleSettings["AddNewPlacement"];
                    }
                    else
                    {
                        ddlAddNewPlacement.SelectedValue = "Top";
                    }

                    if (!String.IsNullOrEmpty((string)ModuleSettings["OrderBy"]))
                    {
                        ddlOrderBy.SelectedValue = (string)ModuleSettings["OrderBy"];
                    }
                    else
                    {
                        ddlOrderBy.SelectedValue = "TestimonialDateDesc";
                    }
                    
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        /// <summary>
        /// Update the Settings for the Module
        /// </summary>
        public override void UpdateSettings()
        {
            try
            {
                ModuleController controller = new ModuleController();

                if (string.IsNullOrEmpty((string)Settings["DisplayTemplate"])) {
                     //Build out a default value
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<div class=\"testimonialContainer\">");
                        sb.AppendLine("<div class=\"testimonial\">");
                        sb.AppendLine("[Testimonial] <br />");
                        sb.AppendLine("[FirstName][LastNameInitial] <br />");
                        sb.AppendLine("[Location]");
                        sb.AppendLine("</div>");
                        sb.AppendLine("</div>");
                        Settings["DisplayTemplate"] = sb.ToString();
                }
                controller.UpdateModuleSetting(ModuleId, "DisplayTemplate", txtDisplayTemplate.Text);


                if (string.IsNullOrEmpty((string)Settings["ContainerClass"]))
                {
                    Settings["ContainerClass"] = "veTestimonialRotator";
                }
                controller.UpdateModuleSetting(ModuleId, "ContainerClass", txtContainerClass.Text);

                if(string.IsNullOrEmpty((string)Settings["AddNewLinkClass"])) {
                    Settings["AddNewLinkClass"] = "veTestimonialAddNewLink";
                }
                controller.UpdateModuleSetting(ModuleId, "AddNewLinkClass", txtAddNewLinkClass.Text);

                if (string.IsNullOrEmpty((string)Settings["AddNewLinkText"]))
                {
                    Settings["AddNewLinkText"] = "Add a Testimonial";
                }
                controller.UpdateModuleSetting(ModuleId, "AddNewLinkText", txtAddNewLinkText.Text);

                if (string.IsNullOrEmpty((string)Settings["NumberToDisplay"]))
                {
                    Settings["NumberToDisplay"] = "10";
                }
                controller.UpdateModuleSetting(ModuleId, "NumberToDisplay", txtNumberToDisplay.Text); 

                if (string.IsNullOrEmpty((string)Settings["AnimationOption"]))
                {
                    Settings["AnimationOption"] = "vetestimonials.js";
                }
                controller.UpdateModuleSetting(ModuleId, "AnimationOption", ddlAnimationOption.SelectedValue);

                if (string.IsNullOrEmpty((string)Settings["AddNewPlacement"]))
                {
                    Settings["AddNewPlacement"] = "Top";
                }
                controller.UpdateModuleSetting(ModuleId, "AddNewPlacement", ddlAddNewPlacement.SelectedValue);

                if (string.IsNullOrEmpty((string)Settings["OrderBy"]))
                {
                    Settings["OrderBy"] = "TestimonialDateDesc";
                }
                controller.UpdateModuleSetting(ModuleId, "OrderBy", ddlOrderBy.SelectedValue);

            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

    }
}